package com.dhcc.cdc.busi.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.dao.*;
import com.dhcc.cdc.busi.entity.CdcEpisodeEntity;
import com.dhcc.cdc.busi.entity.CdcLabVisitEntity;
import com.dhcc.cdc.busi.entity.CdcLocationEntity;
import com.dhcc.cdc.busi.entity.CdcUserEntity;
import com.dhcc.cdc.busi.service.ICdcLabVisitService;
import com.dhcc.cdc.sys.dao.CdcDataMapper;
import com.dhcc.cdc.sys.entity.CdcDataEntity;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.util.CommonUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 标本送检记录表数据服务层接口
 * 
 * @ClassName: LabVisitServiceImpl
 * @Description: TODO
 * @author jumper
 * @date 2021-04-26 16:21:40
 */
@Service("labVisitService")
public class CdcLabVisitServiceImpl extends BaseServiceImpl<CdcLabVisitMapper, CdcLabVisitEntity> implements ICdcLabVisitService {

	@Autowired
	private CdcEpisodeMapper cdcEpisodeMapper;
	
	@Autowired
    private CdcDictionaryMapper cdcDictionaryMapper;
	
	@Autowired
	private CdcUserMapper cdcUserMapper;
	
	@Autowired
	private CdcDataMapper cdcDataMapper;
	
	@Autowired
    private CdcLocationMapper cdcLocationMapper;
    
	@Override
	public List<CdcLabVisitEntity> page(Page<CdcLabVisitEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<CdcLabVisitEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}
	@Override
	public String handleXmlData(String dataStr) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Document doc = null;
        try {
            doc = DocumentHelper.parseText(dataStr); // 将字符串转为XML
            Element rootElt = doc.getRootElement(); // 获取根节点
            Iterator<Element> iter = rootElt.elementIterator("labvisit"); // 获取根节点下的子节点labvisit
            // 遍历labvisit节点
            while (iter.hasNext()) {
                Element labEle = iter.next();
                String xcode = labEle.elementTextTrim("xcode"); //索引码
                String episodeid = labEle.elementTextTrim("episodeid"); //就诊记录
                String specvisitnum = labEle.elementTextTrim("specvisitnum"); //检验号|标本号
                String specimen = labEle.elementTextTrim("specimen"); //标本
                String lablocid = labEle.elementTextTrim("lablocid"); //申请科室|送检科室
                String labwardid = labEle.elementTextTrim("labwardid"); //申请病区|送检病区
                String coldatestr = labEle.elementTextTrim("coldate"); //采集日期
                String colusername = labEle.elementTextTrim("colusername"); //采集人
                String recdatestr = labEle.elementTextTrim("recdate"); //接收日期
                String recusername = labEle.elementTextTrim("recusername"); //接受人
                String updatetimestr= labEle.elementTextTrim("updatetime"); //更新时间
                String updateuserid= labEle.elementTextTrim("updateuserid"); //更新人
                String opercode= labEle.elementTextTrim("opercode"); //操作码
                CdcLabVisitEntity labVist = new CdcLabVisitEntity();
                labVist.setXcode(xcode);
                //获取basedataId
    			CdcDataEntity cdcDataEntityBase = new CdcDataEntity();
    			cdcDataEntityBase.setCode("HisBase");
    			cdcDataEntityBase.setIsActive(1);
    			cdcDataEntityBase = cdcDataMapper.selectOne(cdcDataEntityBase);
        		Long basedataId = cdcDataEntityBase.getId();
        		//获取dataid
            	CdcDataEntity cdcDataEntity = new CdcDataEntity();
            	cdcDataEntity.setCode("LabData");
            	cdcDataEntity.setIsActive(1);
            	cdcDataEntity = cdcDataMapper.selectOne(cdcDataEntity);
        		Long dataId = cdcDataEntity.getId();
                if(CommonUtil.isNotEmpty(updatetimestr)) {
            		Date updatetime = sdf.parse(updatetimestr);
            		labVist.setUpdateDate(updatetime);
            	}
            	//获取更新人
            	if(CommonUtil.isNotEmpty(updateuserid)) {
            		CdcUserEntity cdcUserEntity = new CdcUserEntity();
            		cdcUserEntity.setXcode(updateuserid);
            		cdcUserEntity.setDataId(basedataId);
            		cdcUserEntity.setIsActive(1);
            		cdcUserEntity = cdcUserMapper.selectOne(cdcUserEntity);
            		//判断人员存在不存在
            		Long updateuser = null;
            		if(CommonUtil.isEmpty(cdcUserEntity)) {//不存在  保存到用户表里面
            			cdcUserEntity = new CdcUserEntity();
            			cdcUserEntity.setXcode(updateuserid);
                		cdcUserEntity.setIsActive(1);
            			cdcUserEntity.setCode("newuser"+updateuserid);
            			cdcUserEntity.setName("newuser"+updateuserid);
                		cdcUserEntity.setDataId(basedataId);
                		//保存用户
                		cdcUserMapper.insert(cdcUserEntity);
                		updateuser = cdcUserEntity.getId();
            		}else {
            			updateuser = cdcUserEntity.getId();
            		}
            		labVist.setUpdateBy(updateuser);
            	}
            	
                if("1".equals(opercode)) {//保存/修改
                	//获取episodeid
                	CdcEpisodeEntity cdcEpisodeEntity = new CdcEpisodeEntity();
                	cdcEpisodeEntity.setXcode(episodeid);
                	cdcEpisodeEntity=cdcEpisodeMapper.selectOne(cdcEpisodeEntity);//就诊信息没在库中？？？
                	Long epsid = cdcEpisodeEntity.getId();
                	labVist.setEpisodeId(epsid);
                	if(CommonUtil.isNotEmpty(specvisitnum)) {
                		labVist.setSpecVisitNum(specvisitnum);
                	}
                	if(CommonUtil.isNotEmpty(specimen)) {
                		labVist.setSpecimen(specimen);
                	}
                	if(CommonUtil.isNotEmpty(lablocid)) {
                		CdcLocationEntity cdcLocationEntity = new CdcLocationEntity();
                		cdcLocationEntity.setXcode(lablocid);
                		cdcLocationEntity.setDataId(basedataId);
                		cdcLocationEntity.setIsActive(1);
                		cdcLocationEntity = cdcLocationMapper.selectOne(cdcLocationEntity);
                		if(CommonUtil.isNotEmpty(cdcLocationEntity)) {
                			Long labloclongid = cdcLocationEntity.getId();
                			labVist.setLabLocId(labloclongid);
                		}
                	}
                	if(CommonUtil.isNotEmpty(labwardid)) {
                		CdcLocationEntity cdcLocationEntity = new CdcLocationEntity();
                		cdcLocationEntity.setXcode(labwardid);
                		cdcLocationEntity.setDataId(basedataId);
                		cdcLocationEntity.setIsActive(1);
                		cdcLocationEntity = cdcLocationMapper.selectOne(cdcLocationEntity);
                		if(CommonUtil.isNotEmpty(cdcLocationEntity)) {
                			Long labwardlongid = cdcLocationEntity.getId();
                			labVist.setLabWardId(labwardlongid);
                		}
                	}
                	if(CommonUtil.isNotEmpty(coldatestr)) {
                		Date coldate = sdf.parse(coldatestr);
                		labVist.setColDate(coldate);
                	}
                	if(CommonUtil.isNotEmpty(colusername)) {
                		labVist.setColUserName(colusername);
                	}
                	if(CommonUtil.isNotEmpty(recdatestr)) {
                		Date recdate = sdf.parse(recdatestr);
                		labVist.setRecDate(recdate);
                	}
                	if(CommonUtil.isNotEmpty(recusername)) {
                		labVist.setRecUserName(recusername);
                	}
            		labVist.setDataId(dataId);
                	labVist.setIsActive(1);
                }else {
                	labVist.setIsActive(0);
                }
                //先判断是不是有该对象
                Wrapper<CdcLabVisitEntity> wrapper =  new EntityWrapper<CdcLabVisitEntity>();
                wrapper.eq("xcode", labVist.getXcode());
                wrapper.eq("data_id", dataId);
                int count = this.selectCount(wrapper);
                if(count>0) {
                	this.update(labVist, wrapper);
                }else {
                	this.insert(labVist);
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
            return "<response><status>0</status><message>"+e.getMessage()+"</message></response>";
        } catch (Exception e) {
            e.printStackTrace();
            return "<response><status>0</status><message>"+e.getMessage()+"</message></response>";
        }
		return "<response><status>1</status><message>ok</message></response>";
	}

}